#include <bits/stdc++.h>
// 2025/01/24
// tag: 
// Author: Zhang Muen
using namespace std;

int n, m, EnteringDegree[26], ED2[26];
bool edge[26][26];
vector<int> g[26];

bool topologicalSort(int p){
    int cnt = 0, flag = true;
    queue<int> q;
    string s;
    for (int i = 0; i < n; i++)
        if (EnteringDegree[i] == 0)
            q.push(i), cnt++, ED2[i] = 0;
        else
            ED2[i] = EnteringDegree[i];
    if (cnt > 1)
        flag = false;
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        s += u + 'A';
        int cnt2 = 0;
        for (int i = 0; i < g[u].size(); i++)
        {
            int v = g[u][i];
            ED2[v]--;
            if (ED2[v] == 0)
                q.push(v), cnt++, cnt2++;
        }
        if (cnt2 > 1)
            flag = false;
    }
    if (cnt != n){
        cout << "Inconsistency found after " << p << " relations." << endl;
        return true;
    }
    if (flag){
        cout << "Sorted sequence determined after " << p << " relations: " << s << "." << endl;
        return true;
    }
    return false;
}

signed main()
{
    cin >> n >> m;
    for (int i = 1; i <= m; i++){
        string s;
        cin >> s;
        int u = s[0] - 'A', v = s[2] - 'A';
        if (!edge[u][v])
            edge[u][v] = true, EnteringDegree[v]++, g[u].push_back(v);
        if (topologicalSort(i))
            return 0;
    }
    cout << "Sorted sequence cannot be determined." << endl;
    return 0;
}